package com.ag.admin.action;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ag.admin.service.IAuthorizeService;
import com.ag.admin.utils.UserUtil;
import com.ag.bean.TbUser;
import com.ag.common.AjaxNotify;
import com.ag.common.BaseAction;
import com.ag.common.IPermissionService;

/**
 * 系统用户服务Action类，用来获取用户信息等
 * 
 * @author 黎肇明
 * 
 */
@Scope("prototype")
@Controller("UserServAction")
public class UserServAction extends BaseAction implements SessionAware {
	private static final long serialVersionUID = 4898409385454391481L;
	/**
	 * 调用权限服务逻辑组件
	 */
	@Resource(name = "PermissionServiceImpl")
	private IPermissionService pms;
	@Resource(name = "AuthorizeServiceImpl")
	private IAuthorizeService asi;
	/**
	 * 提供Map<String, Object>类型的Session变量，用来保存Struts2注入的Session上下文
	 */
	private Map<String, Object> sessionMap;
	/**
	 * 用户信息返回载体
	 */
	private TbUser tbUser;

	/**
	 * 异步请求结果返回
	 */
	private String ajaxRst;

	@Override
	public void setSession(Map<String, Object> session) {
		this.sessionMap = session;
	}

	/**
	 * 查询当前系统用户信息，用TbUser tbUser承载数据至序列化
	 * 
	 * @return
	 */
	public String checkAcceptStaff() {
		try {
			if (asi.checkRole(ServletActionContext.getRequest().getSession(),
					UserUtil.ACCEPT_STAFF)) {
				ajaxRst = "accept";
			} else {
				ajaxRst = "notaccept";
			}
			return "operate";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 查询当前系统用户信息，用TbUser tbUser承载数据至序列化
	 * 
	 * @return
	 */
	public String findUserInfo() {
		try {
			tbUser = pms.getCurrentUser(sessionMap);
			ajaxRst = AjaxNotify.SUCCESS;
			return "find_user_info";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	/**
	 * 获取登录验证码
	 * 
	 * @return
	 */
	public String findVerifyCode() {
		try {
			ajaxRst = (String) sessionMap.get(UserUtil.VERIFY_CODE);
			return "operate";
		} catch (Exception e) {
			e.printStackTrace();
			ajaxRst = AjaxNotify.EXCEPTION;
			return "operate";
		}
	}

	public TbUser getTbUser() {
		return tbUser;
	}

	public String getAjaxRst() {
		return ajaxRst;
	}

}
